﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using TracedReality.Logging;

namespace Interface.Logging
{
    /// <summary>
    /// A receiver that outputs to a RichTextBox in a Form
    /// </summary>
    public partial class VisualLog : Form, LogReceiver
    {
        public VisualLog()
        {
            InitializeComponent();
        }

        private delegate void AddTextCallBack(string str);
        private void addText(string str)
        {
            if (rtbLog.InvokeRequired)
            {
                AddTextCallBack atcb = new AddTextCallBack(addText);
                this.Invoke(atcb, new object[] { str });
            }
            else
            {
                rtbLog.AppendText(str + "\n");
                rtbLog.ScrollToCaret();
            }
        }

        /// <summary>
        /// Receive a string and add it to the RichTextBox.
        /// If this is not Visible, it will make itself Visible.
        /// </summary>
        /// <param name="str">String to output.</param>
        public void giveString(String str)
        {
            if (!Visible) { Visible = true; }
            addText(str);
        }

        private void VisualLogger_SizeChanged(object sender, EventArgs e)
        {
            rtbLog.Height = Height - rtbLog.Top - 32;
            rtbLog.Width = Width - rtbLog.Left - 10;
        }

        private void VisualLogger_Load(object sender, EventArgs e)
        {
            this.Icon = Icon.FromHandle(Properties.Resources.test.GetHicon());
        }
    }
}
